package common.tool.box.utils.markdown;

import common.tool.box.common.core.domain.model.MarkdownFormatParam;
import lombok.SneakyThrows;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Optional;

/**
 * @author jiazekai 2022年11月11日
 * @ProjectName common tool box
 * MarkdownFormatUtils
 */
public class MarkdownFormatUtils {
    // 正则表达式匹配单个空格
    private static final String SINGLE_SPACE = " ";

    // 正则表达式匹配多个空格
    private static final String MULTIPLE_SPACE = " +";

    // 正则表达式匹配换行符
    private static final String LINE_BREAK = "\r";

    // 正则表达式匹配tab
    private static final String TAB = "\t";

    // markdown语法空格
    private static final String MARKDOWN_SPACE = "&nbsp;";

    // markdown语法四个空格
    private static final String MARKDOWN_FOUR_SPACE = "&nbsp;&nbsp;&nbsp;&nbsp;";

    // markdown语法tab
    private static final String MARKDOWN_TAB = "&emsp;";

    // markdown语法换行
    private static final String MARKDOWN_LINE_BREAK = "<br>";

    /**
     * 格式化空格
     * @author jiazekai 2022/11/11
     */
    public static String formatSpace(String str) {
        return str.replaceAll(SINGLE_SPACE, MARKDOWN_SPACE);
    }

    /**
     * 格式化换行
     * @author jiazekai 2022/11/11
     */
    public static String formatLine(String str) {
        return str.replaceAll(LINE_BREAK, MARKDOWN_LINE_BREAK);
    }

    /**
     * 格式化tab
     * @author jiazekai 2022/11/11
     */
    public static String formatTab(String str) {
        return str.replaceAll(TAB, MARKDOWN_TAB);
    }

    /**
     * 四空格转tab
     * @author jiazekai 2022/11/13
     * @param
     * @return
     */
    public static String fourSpaceToTab(String str) {
        return str.replaceAll(MARKDOWN_FOUR_SPACE, MARKDOWN_TAB);
    }

    /**
     *  MultipleFile文件转字符串
     * @author jiazekai 2022/11/11
     */
    @SneakyThrows
    public static String MultipartFileToString(MultipartFile multipartFile, String charset) {
        InputStreamReader isr;
        BufferedReader br;
        StringBuilder txtResult = new StringBuilder();
        isr = new InputStreamReader(multipartFile.getInputStream(), Optional.ofNullable(charset).orElse("UTF-8"));
        br = new BufferedReader(isr);
        String lineTxt;
        while ((lineTxt = br.readLine()) != null) {
            txtResult.append(lineTxt).append("\r");
        }
        isr.close();
        br.close();
        return txtResult.toString();
    }

    /**
     *  MultipleFile文件转Markdown字符串
     * @author jiazekai 2022/11/11
     */
    @SneakyThrows
    public static String MultipartFileToMarkdown(MultipartFile multipartFile, MarkdownFormatParam param) {
        // 文件转字符串
        String fileToString = MultipartFileToString(multipartFile, param.getCharset());
        // 格式化空格
        if (param.getFormatSpace()) {
            fileToString = formatSpace(fileToString);
        }
        // 格式化换行
        if (param.getFormatLine()) {
            fileToString = formatLine(fileToString);
        }
        // 格式化tab
        if (param.getFormatTab()) {
            fileToString = formatTab(fileToString);
        }
        return fileToString;
    }

}
